package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.DishlavorMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServicelmpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishlavorMapper dishlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    public DishServicelmpl(DishMapper dishMapper) {
        this.dishMapper = dishMapper;
    }

    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishlavorMapper.insertBatch(flavors);
        }

    }

    @Override
    public PageResult pageSelect(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> dishVO = dishMapper.pageSelect(dishPageQueryDTO);
        return new PageResult(dishVO.getTotal(), dishVO.getResult());
    }

    @Transactional
    @Override
    public void delete(List<Long> ids) {
        //判断当前菜品是否能够删除-是否存在起售中的菜品??
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断当前菜品是否被关联了
        List<Long> setmealIds = setmealDishMapper.getsetmealDishByDishIds(ids);
        if (setmealIds != null && setmealIds.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除
        for (Long id : ids) {
            dishMapper.deleteById(id);
            dishlavorMapper.deleteByDishId(id);
        }

    }

    @Override
    public DishVO getByIdWithFlavor(Long id) {
//        //根据ID查询菜品数据
//        Dish dish = dishMapper.getById(id);
//        //根据菜品id查询口味数据
//        List<DishFlavor> dishFlavors = dishlavorMapper.getByDishId(id);
//        DishVO dishVO = new DishVO();
//        BeanUtils.copyProperties(dish, dishVO);
//        dishVO.setFlavors(dishFlavors);
        DishVO dishVO = dishMapper.getByIdWithDishFlavors(id);
        return dishVO;
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);
        dishlavorMapper.deleteByDishId(dishDTO.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDTO.getId()));
        }
        dishlavorMapper.insertBatch(flavors);
    }

    @Override
    public void stopUp(Integer s, Long id) {
        Dish dish = new Dish();
        dish.setStatus(s);
        dish.setId(id);
        dishMapper.update(dish);
        if (s == StatusConstant.DISABLE) {
            //如果是停售操作,还要将包含当前菜品的套餐也要停售
            //TODO 这里没写完!
            ArrayList<Long> dishIds = new ArrayList<>();
            dishIds.add(id);
            //这里是在菜品删除也写过了,得到套餐id的集合
            List<Long> longs = setmealDishMapper.getsetmealDishByDishIds(dishIds);
            if (longs != null && longs.size() > 0) {
                for (Long aLong : longs) {
                    Setmeal build = Setmeal.builder().id(aLong).status(StatusConstant.DISABLE).build();
                    setmealMapper.update11(build);
                }
            }
        }
    }

    @Override
    public List<Dish> selectById(Long categoryId) {
        //       Dish dish = new Dish();
//        dish.setCategoryId(categoryId);
//        dish.setStatus(StatusConstant.ENABLE);
        //以前傻逼妈的这里要是直接传的dish也能用
        List<Dish> dish1 = dishMapper.selectById(categoryId);
        return dish1;
    }

    @Override
    public List<DishVO> selectlist(Long id) {

        List<DishVO> list = dishMapper.list(id);
        return  list;

    }
}
